﻿// ***********************************************************************
// Assembly         : ELS
// Created          : 10-20-2012
//
// Last Modified On : 10-22-2012
// ***********************************************************************
// <copyright file="TemplateController.cs" company="">
//     Copyright (c) . All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Data;
using Microsoft.Win32;
using System.Windows.Forms;
using ELS.User;

namespace ELS.Template
{
    /// <summary>
    /// Class TemplateController
    /// </summary>
    public class TemplateController
    {
        /// <summary>
        /// The template data access
        /// </summary>
        TemplateDataAccess templateDataAccess = new TemplateDataAccess();

        internal int InsertTemplate(TemplateDocument td)
        {
            return templateDataAccess.InsertTemplate(td);
        }

        internal int UpdateTemplate(TemplateDocument td)
        {
            return templateDataAccess.UpdateTemplate(td);
        }

        /// <summary>
        /// Gets the total number of templates in system
        /// </summary>
        internal int CountAllTemplates()
        {
            return templateDataAccess.CountAllTemplates();
        }

        /// <summary>
        /// Gets the total number of templates in system by keyword
        /// </summary>
        internal int CountAllTemplatesByKeyword(string keyword)
        {
            return templateDataAccess.CountAllTemplatesByKeyword(keyword);
        }

        /// <summary>
        /// Gets the total number of templates in system
        /// </summary>
        internal DataTable GetAllTemplatesPaging(int mintPageSize, int intSkip)
        {
            DataTable dtAllTemplatesPaging = new DataTable();
            dtAllTemplatesPaging = templateDataAccess.GetAllTemplatesPaging(mintPageSize, intSkip);
            return dtAllTemplatesPaging;
        }

        /// <summary>
        /// Gets the total number of template in system by searching keyword
        /// </summary>
        internal DataTable GetAllAvailableTemplatesByKeywordPaging(string keyword, int mintPageSize, int intSkip)
        {
            DataTable dtAllTemplatesPaging = new DataTable();
            dtAllTemplatesPaging = templateDataAccess.GetAllTemplatesByKeywordPaging(keyword, mintPageSize, intSkip);
            return dtAllTemplatesPaging;
        }        

        /// <summary>
        /// Deletes the selected templates.
        /// </summary>
        /// <param name="list">The list.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
        internal bool DeleteSelectedTemplate(List<string> list)
        {
            bool isDeleteTemplateSuccess = false;
            //isDeleteTemplateSuccess = templateDataAccess.DeleteTemplate(list) > 0;
            return isDeleteTemplateSuccess;
        }

        internal void LoadAllDocumentTypesAndDefault(ComboBox comboBox) {

            DataTable dtAllDocumentTypes = new DataTable();
            dtAllDocumentTypes = templateDataAccess.GetAllDocumentTypes();
            DataRow dr = dtAllDocumentTypes.NewRow();
            dr.ItemArray = new object[2] {0, "--Chọn một đối tượng--" };
            dtAllDocumentTypes.Rows.InsertAt(dr, 0);

            comboBox.DataSource = dtAllDocumentTypes;
            comboBox.DisplayMember = "DOCUMENT_TYPE_TITLE";
            comboBox.ValueMember = "DOCUMENT_TYPE_ID";
            comboBox.Tag = "DOCUMENT_TYPE_ID";
        }

        internal void LoadAllDocumentTypes(ComboBox comboBox)
        {

            DataTable dtAllDocumentTypes = new DataTable();
            dtAllDocumentTypes = templateDataAccess.GetAllDocumentTypes();

            comboBox.DataSource = templateDataAccess.GetAllDocumentTypes();
            comboBox.DisplayMember = "DOCUMENT_TYPE_TITLE";
            comboBox.ValueMember = "DOCUMENT_TYPE_ID";
            comboBox.Tag = "DOCUMENT_TYPE_ID";
        }

        internal TemplateDocument GetTemplateInfo(string selectedTemplateID)
        {
            TemplateDocument selectedTemplate = new TemplateDocument();
            try
            {
                DataTable dtTemplate = new DataTable();
                dtTemplate = templateDataAccess.GetTemplateInfo(selectedTemplateID);
                if (dtTemplate.Rows.Count > 0)
                {
                    selectedTemplate.Id = dtTemplate.Rows[0]["TemplateID"].ToString();
                    selectedTemplate.Title = dtTemplate.Rows[0]["TemplateTitle"].ToString();
                    selectedTemplate.Description = dtTemplate.Rows[0]["TemplateDescription"].ToString();
                    selectedTemplate.DocumentTypeId = dtTemplate.Rows[0]["DocumentTypeID"].ToString();

                    return selectedTemplate;
                }
                else
                    return selectedTemplate;
            }
            catch (Exception e)
            {
                return selectedTemplate;
            }
        }

        internal int InsertCategory(string content, string description)
        {
            return templateDataAccess.InsertCategory(content, description);
        }

        internal int InsertQuestion(string content, string questionNo, int questionTypeId)
        {
            return templateDataAccess.InsertQuestion(content, questionNo, questionTypeId);
        }

        internal int InsertAnswer(string content, int answerTypeId)
        {
            return templateDataAccess.InsertAnswer(content, answerTypeId);
        }

        internal int InsertCategoryQuestionRelationship(int categoryId, int questionId, int orderInCategory)
        {
            return templateDataAccess.InsertCategoryQuestionRelationship(categoryId, questionId, orderInCategory);
        }

        internal int InsertQuestionAnswerRelationship(int questionId, int answerId, int orderInQuestion) {
            return templateDataAccess.InsertQuestionAnswerRelationship(questionId, answerId, orderInQuestion);
        }

        internal TemplateQuestion GetPreviousQuestion(int currentQuestionId)
        {
            TemplateQuestion selectTemplateQuestion = new TemplateQuestion();
            try
            {
                DataTable dtTemplateQuestion = new DataTable();
                dtTemplateQuestion = templateDataAccess.GetPreviousQuestion(currentQuestionId);
                if (dtTemplateQuestion.Rows.Count > 0)
                {
                    selectTemplateQuestion.Id = dtTemplateQuestion.Rows[0]["ID"].ToString();
                    selectTemplateQuestion.Content = dtTemplateQuestion.Rows[0]["CONTENT"].ToString();
                    selectTemplateQuestion.QuestionNo = dtTemplateQuestion.Rows[0]["QUESTION_NO"].ToString();
                    selectTemplateQuestion.QuestionTypeId = dtTemplateQuestion.Rows[0]["QUESTION_TYPE_ID"].ToString();                    
                }
                else
                {
                    selectTemplateQuestion.Id = "-1";                    
                }
                return selectTemplateQuestion;
            }
            catch (Exception e)
            {
                selectTemplateQuestion.Id = "-1";
                return selectTemplateQuestion;
            }
        }

        internal TemplateQuestion GetNextQuestion(int currentQuestionId)
        {
            TemplateQuestion selectTemplateQuestion = new TemplateQuestion();
            try
            {
                DataTable dtTemplateQuestion = new DataTable();
                dtTemplateQuestion = templateDataAccess.GetNextQuestion(currentQuestionId);
                if (dtTemplateQuestion.Rows.Count > 0)
                {
                    selectTemplateQuestion.Id = dtTemplateQuestion.Rows[0]["ID"].ToString();
                    selectTemplateQuestion.Content = dtTemplateQuestion.Rows[0]["CONTENT"].ToString();
                    selectTemplateQuestion.QuestionNo = dtTemplateQuestion.Rows[0]["QUESTION_NO"].ToString();
                    selectTemplateQuestion.QuestionTypeId = dtTemplateQuestion.Rows[0]["QUESTION_TYPE_ID"].ToString();
                }
                else
                {
                    selectTemplateQuestion.Id = "-1";
                }
                return selectTemplateQuestion;
            }
            catch (Exception e)
            {
                selectTemplateQuestion.Id = "-1";
                return selectTemplateQuestion;
            }
        }

        internal List<TemplateAnswer> GetAnswersByQuestionId(string questionId)
        {
            List<TemplateAnswer> listAnswers = new List<TemplateAnswer>();
            TemplateAnswer ta = null;
            DataTable dtListAnswers = templateDataAccess.GetAnswersByQuestionId(questionId);
            for (int i = 0; i < dtListAnswers.Rows.Count; i++)
            {
                ta = new TemplateAnswer();
                ta.Id = dtListAnswers.Rows[i]["ID"].ToString();
                ta.Content = dtListAnswers.Rows[i]["CONTENT"].ToString();
                ta.AnswerTypeId = dtListAnswers.Rows[i]["ANSWER_TYPE_ID"].ToString();
                ta.OrderInQuestion = dtListAnswers.Rows[i]["ORDER_IN_QUESTION"].ToString();

                listAnswers.Add(ta);
            }
            return listAnswers;
        }

        internal int GetCategoryIdByTitle(string title, int templateId)
        {
            return templateDataAccess.GetCategoryIdByTitle(title, templateId);
        }
    }    
 }

